package com.situ.company.score.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


import com.situ.company.score.dao.IScoreDao;
import com.situ.company.score.model.ScoreModel;
import com.situ.company.util.JDBCUtil;

public class ScoreDaoImpl implements IScoreDao {

	private String table = "score";
	private Connection conn;
	private PreparedStatement ps;
	
	@Override
	public Integer insert(ScoreModel model) {
		StringBuffer sql = new StringBuffer(" insert into ");
		sql.append( table ).append( "( code_emp,code_pro,score )" ).append( "values(?,?,?)" );
		
		try {
		conn = JDBCUtil.getConnection();
		ps = conn.prepareStatement(sql.toString());
		ps.setString(1, model.getCodeEmp());
		ps.setString(2, model.getCodePro());
		ps.setString(3, model.getScore());
		return ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, ps);
		}
		
		return null;
	}

	@Override
	public Integer deleteByCode(ScoreModel model) {
//		StringBuffer sql = new StringBuffer( " delete from " );
//		sql.append( table ).append( " where " ).append( " code " ).append( " = " ).append("?");
//		
//		try {
//		conn = JDBCUtil.getConnection();
//		ps = conn.prepareStatement(sql.toString());
//		ps.setString(1, model.getCode());
//		return ps.executeUpdate();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}finally {
//			JDBCUtil.close(conn, ps);
//		}
//		
		return null;
	}

	@Override
	public Integer delete(ScoreModel model) {
		StringBuffer sql = new StringBuffer(" delete from ");
		sql.append( table );
		List<Object> values = appendWhere(sql,model);
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString());
			for(int i=0;i<values.size();i++) {
				ps.setObject(i+1, values.get(i));
				return ps.executeUpdate();
			}
		} catch (Exception e) {
			
		} finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}

	@Override
	public Integer updateAll(ScoreModel model) {
		StringBuffer sql = new StringBuffer(" update ");
		sql.append( table ).append(" set code_Pro=?,Score=? where code_Emp=? ");
		
		try {
		conn = JDBCUtil.getConnection();
		ps = conn.prepareStatement(sql.toString());
		ps.setString(1, model.getCodePro());
		ps.setString(2, model.getScore());
		ps.setString(3, model.getCodeEmp());
		return ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, ps);
		}
		
		return null;
	}

	@Override
	public Integer updateActive(ScoreModel model) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<ScoreModel> selectList(ScoreModel model) {
		StringBuffer sql = new StringBuffer(" select id,");
		sql.append( " code_emp,(select name from employee where code_emp = code) name_Emp,code_pro,(select name from project where code_pro = code) name_Pro ,score " ).append( " from " ).append( table );
		List<Object> values = appendWhere(sql,model);
		ResultSet rs =null;
		List<ScoreModel> result = new ArrayList<>();
		try {
			conn=JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString()); 
			for(int i=0;i<values.size();i++) 
				ps.setObject(i+1, values.get(i));
			rs =  ps.executeQuery();
			while(rs.next()) {//ORM映射
				ScoreModel m1 = new ScoreModel();
			  m1.setId(rs.getInt("id"));
			  m1.setCodeEmp(rs.getString("code_emp"));
			  m1.setCodePro(rs.getString("code_pro"));
			  m1.setScore(rs.getString("score"));
			  m1.setNameEmp(rs.getString("name_Emp"));
			  m1.setNamePro(rs.getString("name_Pro"));
			  //m1.setCount(rs.getString("count"));
			 
			  result.add(m1);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.close(conn, ps);
		}
		return result;
	}
	
	private List<Object> appendWhere(StringBuffer sql, ScoreModel model) {
		sql.append(" where 1=1 ");
		List<Object> values = new ArrayList<>();
		String codeEmp = model.getCodeEmp();
		if(codeEmp!=null&&!codeEmp.isEmpty()) {
			sql.append(" and code_emp like ? ");
			values.add(codeEmp);
		}
		String codePro = model.getCodePro();
		if(codePro!=null&&!codePro.isEmpty()) {
			sql.append(" and code_pro like ? ");
			values.add(codePro);
		}
		String score = model.getScore();
		if(score!=null&&!score.isEmpty()) {
			sql.append(" and score like ? ");
			values.add(score);
	}
		if(model.isPageOn()) {
			sql.append(" limit ?,? ");
			values.add(model.getRowStart());
			values.add(model.getPageLimit());
		}
		return values;
	}
	@Override
	public ScoreModel selectModel(ScoreModel model) {
		StringBuffer sql = new StringBuffer( " select id, " );
		sql.append( "code_emp,code_pro,score" ).append(" from ").append( table ).append( " where code_emp=? " ) ;
		
		ResultSet rs =null;
		
		try {
			conn=JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql.toString()); 
			ps.setString(1, model.getCodeEmp());
			
			rs =  ps.executeQuery();
			if(rs.next()) {
				ScoreModel m1 = new ScoreModel();
			  m1.setId(rs.getInt("id"));
			  m1.setCodeEmp(rs.getString("code_emp"));
			  m1.setCodePro(rs.getString("code_pro"));
			  m1.setScore(rs.getString("score"));
			 
			  return m1;
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtil.close(conn, ps);
		}
		return null;
	}

	@Override
	public Integer selectCount(ScoreModel model) {
		StringBuffer sql = new StringBuffer(" select count(1) from score ");
		List<Object> vals = appendWhere(sql, model);
		return JDBCUtil.queInt(sql.toString(), vals);
		
	}

}
